<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Matching Clauses
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot025.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot021.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot027.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc57">5.5</A>&nbsp;&nbsp;Matching Clauses</H2>
<A NAME="matching"></A>
<A NAME="@default192"></A>
<A NAME="@default193"></A>
<A NAME="@default194"></A>
<A NAME="@default195"></A>
When Prolog systems look for clauses that match a given call,
they use full unification of the goal with the clause head
(but usually without the occur check).
Sometimes it is useful or necessary to use <I>pattern matching</I>
instead of full unification, i.e. during the matching
only variables in the clause head can be bound, the call
variables must not be changed.
This means that the call must be an instance of the
clause head.<BR>
<BR>
The operator <CODE>-?-&gt;</CODE> at the beginning of the clause
body specifies that one-way matching should be used
instead of full unification in the clause head:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
p(f(X)) :-
    -?-&gt;
    q(X).
</PRE></BLOCKQUOTE>
Using the <CODE>?-</CODE> operator in the neck of the clause (instead of
<CODE>:-</CODE>) is an alternative way of expressing the same, so the following
is equivalent to the above:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
p(f(X)) ?-
    q(X).
</PRE></BLOCKQUOTE>
Matching clauses are not supported in dynamic clauses. A runtime error
(calling an undefined procedure &minus;?&minus;&gt;/1) will be raised when
executing dynamic code that has a matching clause head.<BR>
<BR>
Pattern matching can be used for several purposes:
<UL CLASS="itemize"><LI CLASS="li-itemize">
Generic pattern matching when looking for clauses
whose heads are more general than the call.<BR>
<BR>
<LI CLASS="li-itemize">Decomposing <I>attributed variables</I> [<A HREF="umsroot159.html#eclipseext"><CITE>4</CITE></A>].
When an attributed variable occurs in the head of a matching clause,
it is not unified with the call argument (which would trigger
the unification handlers) but instead, the call argument
is decomposed into the variable and its attribute(s):
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
get_attr(X{A}, Attr) :-
    -?-&gt;
    A = Attr.
</PRE></BLOCKQUOTE>
This predicate can be used to return the attribute of a given
attributed variable and fail if it is not one.<BR>
<BR>
<LI CLASS="li-itemize">Replacing other metalogical operations, e.g. <A HREF="../bips/kernel/typetest/var-1.html"><B>var/1</B></A><A NAME="@default196"></A>
test. Since a nonvariable in the head of a matching clause
matches only a nonvariable, explicit variable tests and/or cuts
may become obsolete.
</UL>
If some argument positions of a matching clause are declared
as <B>output</B> in a mode declaration, then they are not
unified using pattern matching but normal unification,
in this case then the variable is normally bound.
The above example can thus be also written as
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
:- mode get_attr(?, -).
get_attr(X{A}, A) :-
    -?-&gt;
    true.
</PRE></BLOCKQUOTE>
but in this case it must not be called with its second argument
already instantiated.<BR>
<BR>
<HR>
<A HREF="umsroot025.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot021.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot027.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
